package com.smarthome.web.property.controller.propertymanager;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarthome.domain.enums.ServiceStatusEnum;
import com.smarthome.common.tools.EnumUtil;
import com.smarthome.core.propertymanager.PropertyServiceFeedbackService;
import com.smarthome.core.propertymanager.PropertyServiceService;
import com.smarthome.domain.bean.ShService;
import com.smarthome.domain.bean.ShServiceFeedback;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.web.property.utils.UserHolder;

/**
 * 物业服务
 * 
 * @author hgj
 * 
 */
@Controller
@RequestMapping("/propertyServiceController")
public class PropertyServiceController {

	@Resource
	private PropertyServiceService propertyServiceService;
	@Resource
	private PropertyServiceFeedbackService feedbackService;

	/**
	 * 获取用户数据表格
	 * 
	 * @return
	 */
	@RequestMapping("/dataGrid")
	@ResponseBody
	public DataGrid dataGrid(PageHelper ph, ShService service) {
		Long residenceId = UserHolder.getCurrentResidenceId();
		if(!UserHolder.isSuperResidence()) {
			service.setResidenceId(residenceId);
		}
		return propertyServiceService.dataGrid(ph, service);
	}

	/**
	 * 获取反馈数据表格
	 * 
	 * @return
	 */
	@RequestMapping("/feedbackDataGrid")
	@ResponseBody
	public DataGrid feedbackDataGrid(PageHelper ph, Long id) {
		return feedbackService.dataGrid(ph, id);
	}

	/**
	 * 跳转到查看反馈页面
	 * 
	 * @return
	 */
	@RequestMapping("/showFeedback")
	public String showFeedback(HttpServletRequest request, Long id) {
		request.setAttribute("id", id);
		return "/propertyManager/propertyServiceShow";
	}

	/**
	 * 跳转到添加反馈页面
	 * 
	 * @return
	 */
	@RequestMapping("/showAddFeedbackDlg")
	public String showAddFeedbackDlg(HttpServletRequest request, Long id) {
		request.setAttribute("id", id);
		return "/propertyManager/propertyServiceFeedbackAdd";

	}

	/**
	 * 跳转到修改服务状态页面
	 * 
	 * @return
	 */
	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request, Long id) {
		request.setAttribute("service",
				propertyServiceService.queryPropertyServiceById(id));
		request.setAttribute("serviceType",
				EnumUtil.getEnumList(ServiceStatusEnum.class));
		return "/propertyManager/propertyServiceEdit";
	}

	/**
	 * 修改服务状态
	 * 
	 * @return
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public Result edit(ShService service) {
		propertyServiceService.saveOrEdit(service);
		return Result.success("修改成功！");
	}

	/**
	 * 增加服务反馈
	 * 
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Result add(HttpServletRequest request) {
		String serviceId = request.getParameter("serviceId");
		String content = request.getParameter("content");
		Long userId = UserHolder.getUserId();
		ShServiceFeedback back = new ShServiceFeedback();
		back.setAccountId(userId);
		back.setStatus(1);
		back.setServiceId(Long.parseLong(serviceId));
		back.setContent(content);
		back.setActive(1);
		feedbackService.saveOrEdit(back);
		return Result.success("添加反馈成功！");
	}
}
